<template>
    <div>
        <Form ref="formInline" :model="query" inline>
            <FormItem prop="username">
                <Input type="text" v-model="query.name" placeholder="角色名称">
                    <Icon type="ios-person-outline" slot="prepend"></Icon>
                </Input>
            </FormItem>
            <FormItem>
                <Button type="primary" @click="search">查询</Button>
                <Button type="warning" @click="add">新增</Button>
            </FormItem>
        </Form>
        <Table border :columns="columns" :data="tableData">
            <template slot-scope="{ row, index }" slot="action">
                <Button type="success" size="small" style="margin-right: 5px" @click="authority(row.id)">权限</Button>
                <Button type="primary" size="small" style="margin-right: 5px" @click="edit(row)">编辑</Button>
                <Button type="error" size="small" @click="remove(row.id)">删除</Button>
            </template>
        </Table>
        <Page :total="total" :current="query.extend.current" :page-size="query.extend.size" show-sizer @on-change="toPage" @on-page-size-change="changeSize"/>

        <Modal v-model="modelShow" :title="modelType===1?'新增':'编辑'">
            <Form
                ref="formValidate"
                :model="formValidate"
                :label-width="80"
            >
                <FormItem label="角色名" prop="name" :rules="{
                    required: true,
                    message: '角色名必填',
                    trigger: 'blur'
                }">
                    <Input v-model="formValidate.name" maxlength="20"/>
                </FormItem>
                <FormItem label="备注" prop="remark">
                    <Input v-model="formValidate.remark" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="角色备注说明"/>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="text" size="large" @click="modelShow=false">取消</Button>
                <Button type="primary" size="large" @click="submit">确定</Button>
            </div>
        </Modal>
        <Modal v-model="authModelShow" title="权限关联">
            <Tree :data="authTreeData" ref="authTree" show-checkbox multiple></Tree>
            <div slot="footer">
                <Button type="text" size="large" @click="authModelShow=false">取消</Button>
                <Button type="primary" size="large" @click="authSubmit">确定</Button>
            </div>
        </Modal>
    </div>
</template>
<script>
import { findRoleMenu } from '../menu/menu'
import { findPage, addAdminRole, updAdminRole, delAdminRole, addAuth } from './role'
export default {
  name: 'role',
  data () {
    return {
      query: {
        name: '',
        extend: {
          current: 1,
          size: 10,
          like: 'name'
        }
      },
      total: 100,
      columns: [
        {
          title: '名称',
          key: 'name'
        },
        {
          title: '备注',
          key: 'remark'
        },
        {
          title: '操作',
          slot: 'action',
          width: 250,
          align: 'center'
        }
      ],
      tableData: [],
      sexType: [{ id: 0, type: '女' }, { id: 1, type: '男' }],
      modelShow: false,
      modelType: 1,
      formValidate: {},
      authModelShow: false,
      authTreeData: [],
      authRoleId: ''
    }
  },
  created () {
    this.findPage()
  },
  methods: {
    edit (row) {
      this.$refs.formValidate.resetFields()
      this.formValidate = Object.assign({}, row)
      this.modelShow = true
      this.modelType = 2
    },
    remove (id) {
      delAdminRole(id).then(res => {
        if (res.code === 401) {
          this.$Message.error(res.msg)
        } else {
          this.$Message.success('删除成功')
          this.search()
        }
        this.modelShow = false
      })
    },
    add () {
      this.$refs.formValidate.resetFields()
      this.formValidate = { age: 20, sex: 1 }
      this.modelShow = true
      this.modelType = 1
    },
    submit () {
      this.$refs.formValidate.validate((valid) => {
        if (valid) {
          if (this.modelType === 1) {
            addAdminRole(this.formValidate).then(res => {
              this.modelShow = false
              this.search()
              this.$Message.success('操作成功')
            })
          } else {
            updAdminRole(this.formValidate).then(res => {
              this.modelShow = false
              this.search()
              this.$Message.success('操作成功')
            })
          }
        }
      })
    },
    search () {
      this.query.extend.current = 1
      this.findPage()
    },
    findPage () {
      findPage(this.query).then(res => {
        if (res.code === 200) {
          this.total = res.data.total
          this.query.extend.current = res.data.current
          this.tableData = res.data.records
        } else { this.$Message.error(res.msg) }
      })
    },
    toPage (current) {
      this.query.extend.current = current
      this.findPage()
    },
    changeSize (size) {
      this.query.extend.current = 1
      this.query.extend.size = size
      this.findPage()
    },
    authority (id) {
      this.authModelShow = true
      findRoleMenu(id).then(res => {
        this.authRoleId = id
        this.authTreeData = res.data
      })
    },
    authSubmit () {
      console.log(this.authRoleId, this.$refs.authTree.getCheckedAndIndeterminateNodes())
      let menuIds = this.$refs.authTree.getCheckedAndIndeterminateNodes().map(o => o.id) || []
      addAuth({ roleId: this.authRoleId, menuIds: menuIds }).then(res => {
        this.authModelShow = false
        this.$Message.success('配置成功')
      })
    }
  }
}
</script>
